package com.jt.common.Thread;


import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

class SingleTon{
    private SingleTon(){ //私有化构造方法

    }
    private static SingleTon singleTon;
    public static SingleTon getInstance() {
        if (singleTon == null) {
            synchronized (SingleTon.class) {
                if (singleTon == null) {
                    singleTon = new SingleTon();
                }
            }
        }
        return singleTon;
    }
}

/**
 * 单例设计线程安全的(双重校验)
 */
public class SingleTonTest {
    public static void main(String[] args) {
        ScheduledExecutorService ses = Executors.newScheduledThreadPool(3);
        Runnable task = new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + SingleTon.getInstance());
            }
        };

        ses.scheduleAtFixedRate(task,1,1, TimeUnit.SECONDS);
    }
}
